package com.wm.web.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.City;
import com.wm.bean.Restaurant;
import com.wm.bean.Scriptorium;
import com.wm.comm.Constant;
import com.wm.comm.PinYinUtil;
import com.wm.comm.Util;
import com.wm.service.CityService;
import com.wm.service.RestaurantService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.ui.OptionsInt;

public class AdminRestaurant extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminRestaurant.class);
	
	private RestaurantService restaurantService;

	public RestaurantService getRestaurantService() {
		return restaurantService;
	}

	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}


	private List isShowList = new ArrayList();
	private List tasteList = new ArrayList();
	private List catenationList = new ArrayList();
	private List validList = new ArrayList();
	
	public List getIsShowList() {
		return isShowList;
	}

	public void setIsShowList(List isShowList) {
		this.isShowList = isShowList;
	}
	
	public List getTasteList() {
		return tasteList;
	}

	public void setTasteList(List tasteList) {
		this.tasteList = tasteList;
	}

	public List getCatenationList() {
		return catenationList;
	}

	public void setCatenationList(List catenationList) {
		this.catenationList = catenationList;
	}

	public List getValidList() {
		return validList;
	}

	public void setValidList(List validList) {
		this.validList = validList;
	}
	

	private void setIsShowListValues(){
		isShowList = Constant.IS_SHOW;
	}
	private void setTasteListValues(){
		tasteList = Constant.RESTAURANT_TASTE;
	}
	private void setCatenationListValues(){
		catenationList.add(new OptionsInt(-1, "请选择"));
		catenationList.add(new OptionsInt(0, "非连锁"));
		catenationList.add(new OptionsInt(1, "连锁"));
	}
	private void setValidListValues(){
		validList.add(new OptionsInt(-1, "请选择"));
		validList.add(new OptionsInt(0, "未审核"));
		validList.add(new OptionsInt(1, "己审核"));
		validList.add(new OptionsInt(1, "黑名单"));
	}
	
	private void initValues(){
		setIsShowListValues();
		setTasteListValues();
		setCatenationListValues();
		setValidListValues();
	}
	
	private void listInitValues(){
		setIsShowListValues();
		setTasteListValues();
		setCatenationListValues();
		setValidListValues();
	}


	private long id;					//主键ID
	private String resName = "";		//饭店名称
	private String resInitials;			//首字母
	//private String resAnglic;			//饭店的简拼
	//private String resPhonetic;		//饭店的全拼
	private String resTaste = "";		//饭店口味 0:没有分类 1:中式 2:西式 3:港式
	private int isCatenation = -1;		//是否连锁 0:非连锁 1:连锁
	private int deliveryPrice;			//起送价
	private String specialMemo;			//特殊说明
	private String summary;				//饭店简介
	private String address;				//地址
	private int distance;				//距离
	private String notice;				//饭店公告
	private String salesPromotion;		//促销信息
	private int score;					//评分
	private int scorePeople;			//平分人数
	private int valid = -1;				//是否有效,审核使用 0:未申核 1:己申核 2:黑名单
	private int isShow = -1;			//是否显示
	private Date showTime;				//显示时间
	private int orderNum;				//排序字段
	private int deliverTime;			//送达时间
	private String phone;				//联系电话
	private String phone2;				//联系电话2
	private String logo;				//logo
	private String logoPath;			//logo地址	
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getResName() {
		return resName;
	}

	public void setResName(String resName) {
		this.resName = resName;
	}

	public String getResInitials() {
		return resInitials;
	}

	public void setResInitials(String resInitials) {
		this.resInitials = resInitials;
	}

	public String getResTaste() {
		return resTaste;
	}

	public void setResTaste(String resTaste) {
		this.resTaste = resTaste;
	}

	public int getIsCatenation() {
		return isCatenation;
	}

	public void setIsCatenation(int isCatenation) {
		this.isCatenation = isCatenation;
	}

	public int getDeliveryPrice() {
		return deliveryPrice;
	}

	public void setDeliveryPrice(int deliveryPrice) {
		this.deliveryPrice = deliveryPrice;
	}

	public String getSpecialMemo() {
		return specialMemo;
	}

	public void setSpecialMemo(String specialMemo) {
		this.specialMemo = specialMemo;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public int getDistance() {
		return distance;
	}

	public void setDistance(int distance) {
		this.distance = distance;
	}

	public String getNotice() {
		return notice;
	}

	public void setNotice(String notice) {
		this.notice = notice;
	}

	public String getSalesPromotion() {
		return salesPromotion;
	}

	public void setSalesPromotion(String salesPromotion) {
		this.salesPromotion = salesPromotion;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getScorePeople() {
		return scorePeople;
	}

	public void setScorePeople(int scorePeople) {
		this.scorePeople = scorePeople;
	}

	public int getValid() {
		return valid;
	}

	public void setValid(int valid) {
		this.valid = valid;
	}

	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}

	public Date getShowTime() {
		return showTime;
	}

	public void setShowTime(Date showTime) {
		this.showTime = showTime;
	}

	public int getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}

	public int getDeliverTime() {
		return deliverTime;
	}

	public void setDeliverTime(int deliverTime) {
		this.deliverTime = deliverTime;
	}		

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPhone2() {
		return phone2;
	}

	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}

	public String getLogo() {
		return logo;
	}

	public void setLogo(String logo) {
		this.logo = logo;
	}

	public String getLogoPath() {
		return logoPath;
	}

	public void setLogoPath(String logoPath) {
		this.logoPath = logoPath;
	}

	private PageList pageList;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}
	/**
	 * 初始页
	 */
	public String index(){
		listInitValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_restaurant.action?resName="+this.resName+"&valid="+this.valid+"&isShow=" + this.isShow);
		this.setPageList(this.getRestaurantService().findRestaurantsByResNameValidIsShow(this.resName,this.valid,this.isShow,"id", 1, pages));
		return SUCCESS;
	}	
	public String add(){
		initValues();
		this.setAction("doadd");
		return INPUT;
	}	
	public String doadd(){
		//添加
		Restaurant restaurant = new Restaurant();
		restaurant.setResName(this.resName);
		restaurant.setResInitials(this.resInitials.toUpperCase());
		restaurant.setResAnglic(PinYinUtil.getPinYinHeadChar(this.resName));
		restaurant.setResPhonetic(PinYinUtil.getPinYin(this.resName));
		restaurant.setResTaste(this.resTaste);
		restaurant.setIsCatenation(this.isCatenation);
		restaurant.setDeliveryPrice(this.deliveryPrice);
		restaurant.setSpecialMemo(this.specialMemo);
		restaurant.setSummary(this.summary);
		restaurant.setAddress(this.address);
		restaurant.setDistance(this.distance);
		restaurant.setNotice(this.notice);
		restaurant.setSalesPromotion(this.salesPromotion);
		restaurant.setScore(this.score);
		restaurant.setScorePeople(this.scorePeople);
		restaurant.setValid(this.valid);
		restaurant.setIsShow(this.isShow);
		restaurant.setShowTime(this.getShowTime().getTime());
		restaurant.setOrderNum(this.orderNum);
		restaurant.setOrderTime(new Date());
		restaurant.setAddTime(new Date());
		restaurant.setDeliverTime(this.deliverTime);
		restaurant.setPhone(this.phone);
		restaurant.setPhone2(this.phone2);
		restaurant.setLogo(this.logo);
		try {
			this.getRestaurantService().saveRestaurant(restaurant);
		} catch (Exception e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	public String update(){
		initValues();
		this.setAction("doupdate");
		Restaurant restaurant = this.getRestaurantService().findRestaurantById(this.id);
		this.setResName(restaurant.getResName());
		this.setResInitials(restaurant.getResInitials());
		//this.setResAnglic(restaurant.getResAnglic());
		//this.setResPhonetic(restaurant.getResPhonetic());
		this.setResTaste(restaurant.getResTaste());
		this.setIsCatenation(restaurant.getIsCatenation());
		this.setDeliveryPrice(restaurant.getDeliveryPrice());
		this.setSpecialMemo(restaurant.getSpecialMemo());
		this.setSummary(restaurant.getSummary());
		this.setAddress(restaurant.getAddress());
		this.setDistance(restaurant.getDistance());
		this.setNotice(restaurant.getNotice());
		this.setSalesPromotion(restaurant.getSalesPromotion());
		this.setScore(restaurant.getScore());
		this.setScorePeople(restaurant.getScorePeople());
		this.setValid(restaurant.getValid());
		this.setIsShow(restaurant.getIsShow());
		this.setOrderNum(restaurant.getOrderNum());
		this.setDeliverTime(restaurant.getDeliverTime());
		this.setPhone(restaurant.getPhone());
		this.setPhone2(restaurant.getPhone2());
		this.setShowTime(long2Date(restaurant.getShowTime()));
		this.setLogoPath(restaurant.getLogoPath());
		return INPUT;
	}
	
	public String doupdate(){
		//添加商品
		Restaurant restaurant = this.getRestaurantService().findRestaurantById(this.id);
		restaurant.setResName(this.resName);
		restaurant.setResInitials(this.resInitials.toUpperCase());
		restaurant.setResAnglic(PinYinUtil.getPinYinHeadChar(this.resName));
		restaurant.setResPhonetic(PinYinUtil.getPinYin(this.resName));
		restaurant.setResTaste(this.resTaste);
		restaurant.setIsCatenation(this.isCatenation);
		restaurant.setDeliveryPrice(this.deliveryPrice);
		restaurant.setSpecialMemo(this.specialMemo);
		restaurant.setSummary(this.summary);
		restaurant.setAddress(this.address);
		restaurant.setDistance(this.distance);
		restaurant.setNotice(this.notice);
		restaurant.setSalesPromotion(this.salesPromotion);
		restaurant.setScore(this.score);
		restaurant.setScorePeople(this.scorePeople);
		restaurant.setValid(this.valid);
		restaurant.setIsShow(this.isShow);
		restaurant.setOrderNum(this.orderNum);
		restaurant.setOrderTime(new Date());
		restaurant.setAddTime(new Date());
		restaurant.setDeliverTime(this.deliverTime);
		restaurant.setPhone(this.phone);
		restaurant.setPhone2(this.phone2);
		try {
			this.getRestaurantService().updateRestaurant(restaurant);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}	
	public String orders(){
		Restaurant restaurant = this.getRestaurantService().findRestaurantById(this.id);
		restaurant.setOrderTime(new Date());
		try {
			this.getRestaurantService().updateRestaurant(restaurant);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	public String del(){
		this.getRestaurantService().removeRestaurantById(this.id);
		return "index";
	}
	/**
	 * 弹出
	 */
	public String pop(){
		listInitValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_restaurant.action?action=pop&resName="+this.resName+"&valid="+this.valid+"&isShow=" + this.isShow);
		this.setPageList(this.getRestaurantService().findRestaurantsByResNameValidIsShow(this.resName,this.valid,this.isShow,"id", 1, pages));
		return "pop";
	}
	private Date long2Date(long t) {
		Calendar cld = Calendar.getInstance();
		cld.set(Util.getYear(t), Util.getMonth(t), Util.getDay(t));
		return cld.getTime();
	}


}
